package mn.slide.dailynews.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import android.content.Context;

public class ImageTool {
	/**
	 * Ямар нэгэн өгөгдлийг (url)-аас уншиж (byte)-ийн олонлогруу хөрвүүлэх 
	 * @param urlString Өгөгдлийг унших (url)
	 * @return Өгөгдлийн хөрвүүлэгдсэн (byte)-ийн олонлог
	 * @throws IOException
	 */
	public static byte[] getDataByteArrayFromUrl(String urlString) throws IOException {
		URL url = new URL(urlString);
		InputStream inputStream = url.openStream();
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int n = 0;
		while (-1 != (n = inputStream.read(buffer))) {
			output.write(buffer, 0, n);
		}
		inputStream.close();
		byte[] data = output.toByteArray();
		return data;
	}
	public static byte[] getDataByteArrayFromUrl(Context context, int res) throws IOException {
		InputStream inputStream = context.getResources().openRawResource(res);
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int n = 0;
		while (-1 != (n = inputStream.read(buffer))) {
			output.write(buffer, 0, n);
		}
		inputStream.close();
		byte[] data = output.toByteArray();
		return data;
	}
}
